package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.biz.MemberOutAccountBiz;
import cn.sumpu.app.guoguojie.dao.MemberOutAccountDao;
import cn.sumpu.app.guoguojie.domain.MemberOutAccount;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;

public class MemberOutAccountBizImpl implements MemberOutAccountBiz {

    @Resource
    private IdentifierBiz       identifierBiz;

    private MemberOutAccountDao memberOutAccountDao;

    public void setMemberOutAccountDao(MemberOutAccountDao memberOutAccountDao) {
        this.memberOutAccountDao = memberOutAccountDao;
    }

    @Override
    public void bindMemberOutAccount(MemberOutAccount outAccount) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("outAccountId", outAccount.getOutAccountId());
        map.put("outAccountType", outAccount.getOutAccountType());
        map.put("member", outAccount.getMember());

        MemberOutAccount m = memberOutAccountDao.getMemberOutAccount(map);
        if (m != null && m.getIdentifier() != null) {
            outAccount.setEnabled(true);
            memberOutAccountDao.UpdateMemberOutAccount(outAccount);
        } else {
            outAccount.setEnabled(true);
            outAccount.setIdentifier(identifierBiz.generateId("member_out_account"));
            memberOutAccountDao.insertMemberOutAccount(outAccount);
        }

    }

    @Override
    public MemberOutAccount checkMemberOutAccount(String outAccountId, String outAccountType) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("outAccountId", outAccountId);
        map.put("outAccountType", outAccountType);
        MemberOutAccount outAccount = memberOutAccountDao.getMemberOutAccount(map);
        return outAccount;
    }

    @Override
    public void UpdateMemberOutAccount(MemberOutAccount outAccount) throws AppBusinessException {
        memberOutAccountDao.UpdateMemberOutAccount(outAccount);

    }

    @Override
    public MemberOutAccount getMemberOutAccount(String member) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("member", member);
        return memberOutAccountDao.getMemberOutAccount(map);
    }

    @Override
    public void setShereWeibo(MemberOutAccount outAccount) throws AppBusinessException {

        memberOutAccountDao.UpdateMemberOutAccount(outAccount);

    }

    @Override
    public MemberOutAccount checkBindOutAccount(MemberOutAccount outAccount) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        if (Common.empty(outAccount)) {
            return null;
        }
        if (!Common.empty(outAccount.getOutAccountId())) {
            map.put("outAccountId", outAccount.getOutAccountId());
        }
        if (!Common.empty(outAccount.getOutAccountType())) {
            map.put("outAccountType", outAccount.getOutAccountType());
        }
        if (!Common.empty(outAccount.getAccessToken())) {
            map.put("accessToken", outAccount.getAccessToken());
        }
        if (!Common.empty(outAccount.getAccessSecret())) {
            map.put("accessSecret", outAccount.getAccessSecret());
        }
        return memberOutAccountDao.getMemberOutAccount(map);
    }

    @Override
    public List<MemberOutAccount> getOutAccountList(String member) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("member", member);
        return memberOutAccountDao.getOutAccountList(map);
    }

    @Override
    public void addMemberOutAccount(MemberOutAccount outAccount) throws AppBusinessException {
        memberOutAccountDao.insertMemberOutAccount(outAccount);
    }

}
